package mju.cxf.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mju.cxf.dto.AdminDTO;
import mju.cxf.dto.AdminLoginDTO;
import mju.cxf.dto.ResetPasswordDTO;
import mju.cxf.entity.Admin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mju.cxf.constant.JwtClaimsConstant;
import mju.cxf.constant.MessageConstant;
import mju.cxf.constant.PasswordConstant;
import mju.cxf.properties.JwtProperties;
import mju.cxf.result.Result;
import mju.cxf.service.AdminService;
import mju.cxf.utils.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import mju.cxf.vo.AdminLoginVO;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员相关接口
 */

@RestController
@RequestMapping("/admin/api/admin")
@Api(tags = "管理员接口列表")
public class AdminController {

    private static final Logger log = LoggerFactory.getLogger(AdminController.class);
    @Autowired
    private AdminService adminService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 管理员登陆接口
     *
     * @param adminLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登陆管理员用户")
    @CrossOrigin
    public Result<AdminLoginVO> loginAdmin(@RequestBody AdminLoginDTO adminLoginDTO) {
        Admin admin = adminService.loginAdmin(adminLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.JWT_ID, admin.getId());
        claims.put(JwtClaimsConstant.ROLE, "ADMIN");

        // 生成令牌
        String token = JwtUtil.createJWT(jwtProperties.getJwtSecretKey(), jwtProperties.getJwtTtl(), claims);

        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .admin(admin)
                .token(token)
                .build();
        //密码设置为空返回
        adminLoginVO.getAdmin().setPassword(null);
        return Result.success(adminLoginVO);
    }

    /**
     * 修改管理员密码
     *
     * @param resetPasswordDTO
     * @return
     */
    @PutMapping("/reset")
    @ApiOperation(value = "修改管理员密码")
    public Result<String> updateAdminPassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        return adminService.updatePassword(resetPasswordDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.UPDATE_PASSWORD_ERROR);
    }

    /**
     * 查询所有管理员
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有管理员")
    public Result<List<Admin>> getAdminAll() {
        return Result.success(adminService.getAllAdmins());
    }

    /**
     * 查询单个管理员
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个管理员")
    public Result<Admin> getAdmin(@PathVariable int id) {
        Admin admin = adminService.getAdminById(id);
        return Result.success(admin);
    }

    /**
     * 新建管理员信息
     *
     * @param admin
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新建管理员")
    public Result<String> createAdmin(@RequestBody AdminDTO admin) {
        admin.setPassword(PasswordConstant.DEFAULT_PASSWORD);
        return adminService.saveAdmin(admin) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 修改管理员信息
     *
     * @param admin
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改管理员信息")
    public Result<String> updateAdmin(@RequestBody AdminDTO admin) {
        return adminService.updateAdmin(admin) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 删除管理员信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除管理员信息", notes = "管理员通过此接口删除管理员信息")
    public Result<String> deleteAdmin(@PathVariable Integer id) {
        return adminService.deleteAdmin(id) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 批量删除管理员信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    @ApiOperation(value = "批量删除管理员信息", notes = "管理员通过此接口批量删除管理员信息")
    public Result<String> deleteAdmins(@RequestBody List<Integer> ids) {
        return adminService.deleteAdmins(ids) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 分页查询管理员信息
     * @return 分页查询结果
     */
    @GetMapping("/pages")
    public Page<Admin> getUsersByUsername(@RequestParam(value = "username",required = false) String username,
                                    @RequestParam(value = "current",defaultValue = "1") int current,
                                    @RequestParam(value = "size",defaultValue = "10") int size) {
        return adminService.selectPageByUsername(username, current, size);
    }
}